using System.Collections.Generic;
using GameFramework.Battle.Core;
using Gameplay.PVE.Config;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using UnityEngine;
using UnityEngine.UI;
using Yoozoo.Managers;

namespace Gameplay.PVE
{
    public class TriggerElement : ElementBase
    {
        private float lastTriggerTime;
        private float nextTriggerTime;
        private float triggerInterval;
        private float triggerDelay;
        private int triggerSkill = 0;
        private int extraSkillId;
        private int extraBulletId;
        private int extraElementId;

        private int changeSkillOldId;
        private int changeSkillNewId;
        private int changeBulletId;
        private int changeElementOldId;
        private int changeElementNewId;

        private int changeSkillType;
        private int changeBulletOldId;
        private int changeBulletNewId;

        private int triggerElementType;
        private int triggerEndElementType;

        public int GetRestTriggerTimes()
        {
            if (triggerInterval == 0)
            {
                return 0;
            }

            var lastTriggerTime = TimeManager.logicTime;
            if (this.lastTriggerTime != 0)
            {
                lastTriggerTime = this.lastTriggerTime;
            }
            float restTime = endTime - lastTriggerTime;
            return (int)(restTime / triggerInterval);
        }

        public void CheckTrigger(ElementBase executedElement)
        {
            //立刻结算剩余伤害
            if (triggerElementType != 0 && executedElement.config.buff_type == triggerElementType)
            {
                int addElementId = GetValue("AddElementId");
                if (addElementId != 0)
                {
                    var elementConfig = PveUtils.GetSkillElementConfig(addElementId);
                    AddElementArg arg = ClassManager.Get<AddElementArg>();
                    arg.id = addElementId;
                    arg.source = source;
                    arg.target = target;
                    arg.elementConfig = elementConfig;
                    arg.bulletPosition = bulletPosition;
                    arg.isCrit = false;
                    arg.sourceSkill = skill;
                    target.Transmit(ETransmitType.AddElement,arg);
                    ClassManager.Free(arg);
                }
               
                //if (target.Data.SpecialElementCount(triggerElementType) > 0)
                {
                    /*if (target.Data.specialBuffs.TryGetValue(triggerEndElementType, out var list))
                    {
                        TriggerElement latest = null;
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (list[i] is TriggerElement triggerElement && (latest == null || latest.GetEndTime() < triggerElement.GetEndTime()))
                            {
                                latest = triggerElement;
                            }
                        }

                        if (latest != null)
                        {
                            int times = latest.GetRestTriggerTimes();
                            int totalTimes = list.Count * times;
                            Debug.LogError("触发了毒素爆发，当前" + list.Count + "层");
                            for (int i = 0; i < list.Count; i++)
                            {
                                list[i].IsFree = true;
                            }
                            list.Clear();
                            for (int i = 0; i < totalTimes; i++)
                            {
                                list.Add(latest);
                            }
                            latest.AddSustainElements();
                            list.Clear();
                        }
                    }*/
                }
            }
        }
        
        public override void Update()
        {
            base.Update();
            if (!IsFree && triggerInterval != 0 && TimeManager.logicTime >= nextTriggerTime)
            {
                AddSustainElements();
                lastTriggerTime = TimeManager.logicTime;
                nextTriggerTime = TimeManager.logicTime + triggerInterval;
            }
        }

        public override void Parse(PveSkillElementConfig config,bool isCrit)
        {
            base.Parse(config,isCrit);
            triggerDelay = GetValue("TriggerDelay") / 1000f;
            nextTriggerTime = TimeManager.logicTime + triggerDelay;
            lastTriggerTime = TimeManager.logicTime;
            triggerInterval = GetValue("TriggerInterval") / 1000f;
            triggerSkill = GetValue("TriggerSkill");
            if (triggerSkill != 0)
            {
                ChangePassiveSkill skill = ClassManager.Get<ChangePassiveSkill>();
                skill.skillId = triggerSkill;
                skill.skillLevel = this.skill.level;
                skill.state = 0;
                target.Transmit(ETransmitType.ChangePassiveSkill,skill);
                ClassManager.Free(skill);
            }

            extraSkillId = GetValue("ExtraSkillId");
            extraBulletId = GetValue("ExtraBulletId");
            extraElementId = GetValue("ExtraElementId");
            if (extraSkillId * extraBulletId * extraElementId != 0)
            {
                ExtraElementArg arg = ClassManager.Get<ExtraElementArg>();
                arg.skillId = extraSkillId;
                arg.bulletId = extraBulletId;
                arg.elementId = extraElementId;
                arg.action = 0;
                target.Transmit(ETransmitType.ChangeExtraElement,arg);
                ClassManager.Free(arg);
                //TimeManager.Instance.DelayCall(2000, () => { Free(); });
            }

            changeSkillType = GetValue("ChangeSkillType");
            changeBulletOldId = GetValue("ChangeBulletOldId");
            changeBulletNewId = GetValue("ChangeBulletNewId");
            if (changeSkillType * changeBulletOldId * changeBulletNewId != 0)
            {
                ChangeBulletArg arg = ClassManager.Get<ChangeBulletArg>();
                arg.changeSkillType = changeSkillType;
                arg.changeBulletOldId = changeBulletOldId;
                arg.changeBulletNewId = changeBulletNewId;
                arg.changeSkillLevel = skill.level;
                arg.action = 0;
                target.Transmit(ETransmitType.ChangeBullet,arg);
                ClassManager.Free(arg);
            }
            /*changeBulletId = GetValue("ChangeBulletId");
            changeElementOldId = GetValue("ChangeElementOldId");
            changeElementNewId = GetValue("ChangeElementNewId");
            if (changeSkillId * changeBulletId * changeElementOldId * changeElementNewId != 0)
            {
                ChangeElementArg arg = ClassManager.Get<ChangeElementArg>();
                arg.skillId = changeSkillId;
                arg.bulletId = changeBulletId;
                arg.oldElementId = changeElementOldId;
                arg.newElementId = changeElementNewId;
                target.Transmit(ETransmitType.ChangeElement,arg);
                ClassManager.Free(arg);
            }*/

            var clearType = GetValue("ClearType");
            if (clearType == 1)
            {
                /*if (target.Data.summonUnit != null)
                {
                    target.Data.summonUnit.Dead();
                }*/
                /*var units = UnitManager.Instance.GetAllUnitByTeam(target.Data.team);
                for (int i = 0; i < units.Count; i++)
                {
                    if (units[i].Data.summonParent == target)
                    {
                        units[i].Dead();
                    }
                }*/
                target.Dead();
            }

            triggerElementType = GetValue("TriggerElementType");
            triggerEndElementType = GetValue("TriggerEndElementType");
        }

        public override void Execute()
        {
            base.Execute();
            //AddSustainElements();
        }

        public override void Free()
        {
            if (triggerSkill != 0)
            {
                ChangePassiveSkill skill = ClassManager.Get<ChangePassiveSkill>();
                skill.skillId = triggerSkill;
                skill.state = 1;
                target.Transmit(ETransmitType.ChangePassiveSkill,skill);
                ClassManager.Free(skill);
            }
            
            if (extraSkillId * extraBulletId * extraElementId != 0)
            {
                ExtraElementArg arg = ClassManager.Get<ExtraElementArg>();
                arg.skillId = extraSkillId; //需要添加额外元素的技能
                arg.bulletId = extraBulletId; //需要添加额外元素的子弹
                arg.elementId = extraElementId; //需要添加的额外元素
                arg.action = 1;
                TimeManager.Instance.DelayCall(GetValue("SkillTime"), () =>
                {
                    target.Transmit(ETransmitType.ChangeExtraElement,arg);
                    ClassManager.Free(arg);
                });
            }
            /*if (changeSkillId * changeBulletId * changeElementOldId * changeElementNewId != 0)
            {
                ChangeElementArg arg = ClassManager.Get<ChangeElementArg>();
                arg.skillId = changeSkillId;
                arg.bulletId = changeBulletId;
                arg.oldElementId = changeElementNewId;
                arg.newElementId = changeElementOldId;
                target.Transmit(ETransmitType.ChangeElement,arg);
                ClassManager.Free(arg);
            }*/
            if (changeSkillType * changeBulletOldId * changeBulletNewId != 0)
            {
                ChangeBulletArg arg = ClassManager.Get<ChangeBulletArg>();
                arg.changeSkillType = changeSkillType;
                arg.changeBulletOldId = changeBulletOldId;
                arg.changeBulletNewId = changeBulletNewId;
                arg.action = 1;
                target.Transmit(ETransmitType.ChangeBullet,arg);
                ClassManager.Free(arg);
            }
            base.Free();
        }

        public void AddSustainElements()
        {
            for (int i = 0; i < 4; i++)
            {
                int id = GetValue("SustainElement" + i);
                if (id != 0)
                {
                    var config = PveUtils.GetSkillElementConfig(id);
                    var arg = ClassManager.Get<AddElementArg>();
                    arg.source = source;
                    arg.target = target;
                    arg.elementConfig = config;
                    arg.sourceSkill = skill;
                    target.Transmit(ETransmitType.AddElement,arg);
                    ClassManager.Free(arg);
                }
            }
        }
        
    }
}